﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MixingStation
{
    public partial class MaterilForm : Form
    {
        private bool m_IsNew = true;
        public bool IsNew
        {
            get { return m_IsNew; }
            set { m_IsNew = value; }
        }

        private string m_Code = "";
        public string Code
        {
            get { return m_Code; }
            set { m_Code = value; }
        }

        private string m_MaterialName = "";
        public string MaterialName
        {
            get { return m_MaterialName; }
            set { m_MaterialName = value; }
        }

        private decimal m_DefaultBuckleRate = 0;
        public decimal DefaultBuckleRate
        {
            get { return m_DefaultBuckleRate; }
            set { m_DefaultBuckleRate = value; }
        }

        private string m_BuckleReasons = "";
        public string BuckleReasons
        {
            get { return m_BuckleReasons; }
            set { m_BuckleReasons = value; }
        }

        public MaterilForm()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCode.Text.Trim()))
            {
                MessageBox.Show("请输入物料编码！");
                return;
            }
            if (string.IsNullOrEmpty(txtMateril.Text.Trim()))
            {
                MessageBox.Show("请输入物料名称！");
                return;
            }
            //if (string.IsNullOrEmpty(txtDefaultBuckleRate.Text.Trim()))
            //{
            //    MessageBox.Show("请输入扣杂比例！");
            //    return;
            //}
            m_Code = this.txtCode.Text.Trim();
            m_MaterialName = this.txtMateril.Text.Trim();

            if (!decimal.TryParse((this.txtDefaultBuckleRate.Text.Trim()), out m_DefaultBuckleRate))
            {
                MessageBox.Show("扣杂比例为数值,请重新输入！");
                return;
            }
            m_BuckleReasons = this.txtBuckleReasons.Text.Trim();
            string sqlStr = "";
            DataTable dt = new DataTable();
            try
            {
                if (m_IsNew)
                {
                    //判断是否存在相同的编码
                    sqlStr = "select * from Material where Code = '" + m_Code + "'";
                    dt = CommonHelper.DoSelectOpreation(sqlStr);
                    if (dt.Rows.Count > 0)
                    {
                        MessageBox.Show("已存在相同的物料编码");
                        return;
                    }
                }
                //判断是否存在相同的物料
                if (m_IsNew)
                    sqlStr = "select * from Material where Name = '" + m_MaterialName + "'";
                else
                    sqlStr = "select * from Material where Name = '" + m_MaterialName + "' and Code <> '" + m_Code + "'";
                dt = CommonHelper.DoSelectOpreation(sqlStr);
                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show("已存在相同的物料名称");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "保存时异常：" + ex.Message);
                return;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void MaterilForm_Load(object sender, EventArgs e)
        {
            if (m_IsNew)
            {
                txtCode.Text = "";
                txtCode.Enabled = true;
                txtMateril.Text = "";
                txtBuckleReasons.Text = "";
                this.Text = "添加物料";
            }
            else
            {
                this.txtCode.Text = m_Code;
                txtCode.Enabled = false;
                this.txtMateril.Text = m_MaterialName;
                this.txtDefaultBuckleRate.Text = m_DefaultBuckleRate.ToString();
                this.txtBuckleReasons.Text = m_BuckleReasons;
                this.Text = "修改物料";
            }
        }
    }
}
